<template>
  <Container class="dealer-sign-edit">
    <!-- 基础设置 -->
    <div class="container-header title title-center" slot="header">
      {{titleText}}合同
      <Button
        size="large"
        class="fl btn-back"
        icon="ios-arrow-back"
        @click="jumpPrevPage"
      >返回</Button>
    </div>
    <DefaultEditItem ref="defalutForm" :readOnly='readOnly' :form="form"></DefaultEditItem>
    <div class="container-footer bg-white" slot="footer">
      <Button type="primary" size="large" class="ml" @click="jumpPrevPage">取消</Button>
      <Button type="primary" size="large" class="ml" @click="onsave" v-if="!readOnly">保存</Button>
    </div>
    <Loader :status="status" @refresh="getData"></Loader>
  </Container>
</template>
<script>
  import DefaultEditItem from "./edit/DefaultEditItem";
  export default {
    name: "",
    components: {
      DefaultEditItem
    },
    data() {
      return {
        status: 'pass',
        isread: false,
        form: {
          name: '',
          id: '',
          sn: '',
          preStartDate: '',
          preEndDate: '',
          startDate: '',
          endDate: '',
          storeTypeId: '',
          content: '',
          remark: '',
          status: '0',
        },
      }
    },
    computed: {
      titleText() {
        let text = '新增'
        if(this.$route.query.id && this.$route.query.mode === 'edit') {
          text = '编辑'
        }
        if(this.$route.query.id && this.$route.query.mode === 'info') {
          text = '查看'
        }
        return text
      },
      readOnly: {
        get() {
          let is = false
          if (this.$route.query.id && this.$route.query.mode === 'info') {
            is = true
          }
          if(this.isread) {
            is = true
          }
          return is
        }
      }
    },
    created() {
      this.form.id = this.$route.query.id || ''
      this.getData()
    },
    methods: {
      getData () {
        let { id } = this.form
        if(!id) return
        this.status = 'loading'
        this.$ajaxGet(`/supplierAgreement/detail/${id}`, { id }).then(res => {
          if (res) {
            this.form = Object.assign(this.form, res)
            this.form.status = res.status.toString()
            if (this.form.status == 1) { // 生效时待生效不能选择
              this.$refs.defalutForm.setDisabed()
            }
            if (this.form.status == 2) { // 失效时不能编辑
              this.isread = true
            }
          }
          this.status = 'pass'
        }).catch(err => {
          console.error(err)
          this.status = 'error'
        })
      },
      jumpPrevPage() {
        this.$router.back()
      },
      onsave() {
        this.$refs["defalutForm"].$refs["form"].validate(valid => {
          if (valid) {
            if (!this.form.id) {
              this.$jsonPost('/supplierAgreement/save', { ...this.form }).then(res => {
                this.$Message.success("新增成功!")
                this.jumpPrevPage()
              })
            } else {
              this.$jsonPost('/supplierAgreement/modify', { ...this.form }).then(res => {
                this.$Message.success("编辑成功!")
                this.getData()
              })
            }
          }
        })
      }
    }
  };
</script>

